/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import model.config.MySQLQuery;
import model.pojo.*;
import model.util.MySQLProviders;
/**
 *
 * @author Admin
 */
public class QuanTriDAO {
    
    public static ArrayList<QuanTriPOJO> LoadDanhSach()
    {
        ArrayList<QuanTriPOJO> qt = new ArrayList<QuanTriPOJO>();
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        String sql = "Select * From QuanTri";
        ResultSet rs = helper.executeQuery(sql);
        try {
            while(rs.next())
            {
                QuanTriPOJO temp = new QuanTriPOJO();
                temp.setTenDangNhap(rs.getString("TenDangNhap"));
                temp.setMatKhau(rs.getString("TenDangNhap"));
                temp.setQuyen(rs.getInt("Quyen"));
                qt.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(QuanTriPOJO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return qt;
    }
    
     public static QuanTriPOJO login(String tenDangNhap, String matKhau) {
      QuanTriPOJO qt = null;
      try {
         String sql = String.format("select *from quantri where tendangnhap=BINARY('%s')"
                 + " and matkhau=MD5('%s')", tenDangNhap, matKhau);
         MySQLProviders helper = new MySQLProviders();
         helper.open();
         ResultSet rs = helper.executeQuery(sql);
         if (rs.next()) {
            qt = new QuanTriPOJO();
            qt.setTenDangNhap(rs.getString("TenDangNhap"));
            qt.setMatKhau(rs.getString("MatKhau"));
            qt.setQuyen(rs.getInt("Quyen"));
         }
         helper.close();
      } catch (Exception ex) {
         System.err.println(ex.getMessage());
      }
      return qt;
   }
     
     public static ArrayList<QuanTriPOJO> getDanhSachCoPhanTrang(int trang) throws FileNotFoundException, IOException {
      ArrayList<QuanTriPOJO> ds = new ArrayList<QuanTriPOJO>(); 
      int soSanPhamTrenTrang = ThamSoDAO.laySoLuongTrenTrang();
      int batDau = (trang - 1) * soSanPhamTrenTrang;
      if(soSanPhamTrenTrang==batDau)
          batDau=1;     
      //mo ket noi
      MySQLProviders helper = new MySQLProviders();
      helper.open();
      
      //doc query tu file text
      //lay cau truy van tu file text
     MySQLQuery mySQLQuery=new MySQLQuery();
     mySQLQuery.Read("PHANTRANG");
     String sql=String.format(mySQLQuery.GetQuery(MySQLQuery.QueryKey.SELECT_ALL_QUANTRI_PHANTRANG),batDau,soSanPhamTrenTrang);
      ResultSet rs = helper.executeQuery(sql);
      try {
         while (rs.next()) {
            QuanTriPOJO temp = new QuanTriPOJO();
            temp.setTenDangNhap(rs.getString("TenDangNhap"));
            temp.setMatKhau(rs.getString("MatKhau"));
            temp.setQuyen(rs.getInt("Quyen"));
            ds.add(temp);
         }
      } catch (SQLException ex) {
         System.out.println(ex.getMessage());
      }
      helper.close();
      return ds;
   }
     
     public static QuanTriPOJO getById(String id){
       QuanTriPOJO obj=new QuanTriPOJO();
       MySQLProviders helper = new MySQLProviders();
       helper.open();
       ResultSet rs = helper.executeQuery("select *from quantri where tendangnhap='"+id+"'");
       try {
           while (rs.next()) {            
            obj.setTenDangNhap(rs.getString("TenDangNhap"));
            obj.setMatKhau(rs.getString("MatKhau"));
            obj.setQuyen(rs.getInt("Quyen"));
            
         }
       }catch(Exception e){
           
       }finally{
           helper.close();
       }
       return obj;
   }
}
